//
//  NovelTypeViewController.m
//  novel
//
//  Created by 高青松 on 2018/8/11.
//  Copyright © 2018年 Tracy. All rights reserved.
//

#import "NovelTypeViewController.h"
#import "WJItemsControlView.h"
#import "NovelSiftViewController.h"
#import "NovelViewModel.h"
@interface NovelTypeViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)WJItemsControlView *itemControlView;
@property (nonatomic, strong) NovelViewModel * novelViewModel ;
@end

@implementation NovelTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1]];
    
    float widht = [UIScreen mainScreen].bounds.size.width;
    float heith = [UIScreen mainScreen].bounds.size.height;
    
    NSArray *array = @[@"精选",@"分类",@"排行"];
    
    
    //3页内容的scrollView
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,44, widht, heith-KRectNavH - KRectStatusH - 44)];
    scroll.bounces = NO;
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.delegate = self;
    scroll.pagingEnabled = YES;
    scroll.contentSize = CGSizeMake(widht*array.count, 100);
    scroll.backgroundColor = [UIColor whiteColor];
    for (int i=0; i<array.count; i++) {
        if (i == 0) {
            NovelSiftViewController * siftVc = [[NovelSiftViewController alloc]init];
            siftVc.view.frame = CGRectMake(0, 0, widht, heith-KRectNavH - KRectStatusH - 44);
            [siftVc.view setBackgroundColor:[UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1]];
            [self addChildViewController:siftVc];
            [scroll addSubview:siftVc.view];
            
        }else{
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(widht*i, 0, widht, heith-100)];
            label.userInteractionEnabled = YES;
            label.text = array[i];
            label.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
            label.textAlignment = NSTextAlignmentCenter;
            
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
            [label addGestureRecognizer:tap];
            
            [scroll addSubview:label];
        }
    }
    [self.view addSubview:scroll];

    //头部控制的segMent
    WJItemsConfig *config = [[WJItemsConfig alloc]init];
    config.itemWidth = widht/3.0;
    config.linePercent = 0.2;
    _itemControlView = [[WJItemsControlView alloc]initWithFrame:CGRectMake(0,0, widht, 44)];
    _itemControlView.tapAnimation = YES;
    _itemControlView.config = config;
    _itemControlView.titleArray = array;
    __weak typeof (scroll)weakScrollView = scroll;
    [_itemControlView setTapItemWithIndex:^(NSInteger index,BOOL animation){
        
        
        [weakScrollView scrollRectToVisible:CGRectMake(index*weakScrollView.frame.size.width, 0.0, weakScrollView.frame.size.width,weakScrollView.frame.size.height) animated:animation];
        
    }];
    [self.view addSubview:_itemControlView];
    
}



- (void)tap{
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float offset = scrollView.contentOffset.x;
    offset = offset/CGRectGetWidth(scrollView.frame);
    [_itemControlView moveToIndex:offset];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    float offset = scrollView.contentOffset.x;
    offset = offset/CGRectGetWidth(scrollView.frame);
    [_itemControlView endMoveToIndex:offset];
    
}

#pragma mark -- lazy load
- (NovelViewModel *)novelViewModel {
    if(!_novelViewModel){
        _novelViewModel = [[NovelViewModel alloc]init];
    }
    return _novelViewModel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
